package cn.xxx.org.service.impl;

import cn.xxx.basic.exception.BusinessException;
import cn.xxx.basic.service.impl.BaseServiceImpl;
import cn.xxx.org.domain.Employee;
import cn.xxx.org.domain.Shop;
import cn.xxx.org.mapper.EmployeeMapper;
import cn.xxx.org.mapper.ShopMapper;
import cn.xxx.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    ShopMapper shopMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public void settledIn(Shop shop) {
        Employee admin = shop.getAdmin();
        //1.对用户进行校验
        Employee employee = employeeMapper.loadByUser(admin.getUsername());
            //判断用户是否存在
        if(employee != null){
            throw new BusinessException("用户已经存在");
        }
        //如果不存在，保存员工数据
        employeeMapper.save(admin);

        //保存门店数据
        shop.setAdmin(admin);
        shopMapper.save(shop);

        //修改管理员门店id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
